package org.example.Swing.xiangmu.login;

import org.example.Swing.handler.ZhuceHandler;

import javax.swing.*;
import java.awt.*;


public class ZhuceView extends JFrame {
    JLabel nameLabel = new JLabel("学生成绩管理系统用户注册", JLabel.CENTER);

    SpringLayout springLayout = new SpringLayout();
    JPanel centerPanel = new JPanel(springLayout);
    JLabel userNameLabel = new JLabel("用户名：");
    JTextField userTxt = new JTextField();
    JLabel pwdLabel = new JLabel("密码：");
    JPasswordField pwdField = new JPasswordField();
    JLabel rollLabel = new JLabel("身份：");
    JTextField rollTet = new JTextField();
    JButton fanhuiBtn = new JButton("返回");
    JButton zhuceBtn = new JButton("注册");


    SystemTray systemTray;
    TrayIcon trayIcon;
    ZhuceHandler zhuceHandler;

    public ZhuceView() {
        super("学生成绩管理系统用户注册");
        zhuceHandler = new ZhuceHandler(this);
        Container contenPane = getContentPane();

        nameLabel.setFont(new Font("华文行楷", Font.PLAIN, 40));
        nameLabel.setPreferredSize(new Dimension(0, 80));

        Font centerFont = new Font("楷体", Font.PLAIN, 20);
        userNameLabel.setFont(centerFont);
        userTxt.setPreferredSize(new Dimension(200, 30));
        pwdLabel.setFont(centerFont);
        pwdField.setPreferredSize(new Dimension(200, 30));
        rollLabel.setFont(centerFont);
        rollTet.setPreferredSize(new Dimension(200, 30));

        fanhuiBtn.setFont(centerFont);
        zhuceBtn.setFont(centerFont);
        //把组件加入面板
        centerPanel.add(userNameLabel);
        centerPanel.add(userTxt);
        centerPanel.add(rollLabel);
        centerPanel.add(rollTet);
        centerPanel.add(pwdLabel);
        centerPanel.add(pwdField);
        //返回按钮点击事件
        fanhuiBtn.addActionListener(zhuceHandler);
        centerPanel.add(fanhuiBtn);
        zhuceBtn.addActionListener(zhuceHandler);
        centerPanel.add(zhuceBtn);


        //布局userNameLabel
        layoutCenter();

        contenPane.add(nameLabel, BorderLayout.NORTH);
        contenPane.add(centerPanel, BorderLayout.CENTER);

        //设置loginBtn为默认按钮
        getRootPane().setDefaultButton(fanhuiBtn);
        LoginView.class.getClassLoader().getResource("");
        setIconImage(null);
        setSize(700, 400);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setResizable(false);
        setVisible(true);
    }

    //弹簧布局
    private void layoutCenter() {
        Spring childWidth = Spring.sum(Spring.sum(Spring.width(userNameLabel), Spring.width(userTxt)), Spring.constant(20));
        int offsetX = childWidth.getValue() / 2;
        //userLabel
        springLayout.putConstraint(SpringLayout.WEST, userNameLabel, -offsetX, SpringLayout.HORIZONTAL_CENTER, centerPanel);
        springLayout.putConstraint(SpringLayout.NORTH, userNameLabel, 20, SpringLayout.NORTH, centerPanel);
        //userpwd
        springLayout.putConstraint(SpringLayout.WEST, userTxt, 20, SpringLayout.EAST, userNameLabel);
        springLayout.putConstraint(SpringLayout.NORTH, userTxt, 0, SpringLayout.NORTH, userNameLabel);
        //pwdLabel
        springLayout.putConstraint(SpringLayout.EAST, pwdLabel, 0, SpringLayout.EAST, userNameLabel);
        springLayout.putConstraint(SpringLayout.NORTH, pwdLabel, 20, SpringLayout.SOUTH, userNameLabel);
        //pwdField
        springLayout.putConstraint(SpringLayout.WEST, pwdField, 20, SpringLayout.EAST, pwdLabel);
        springLayout.putConstraint(SpringLayout.NORTH, pwdField, 0, SpringLayout.NORTH, pwdLabel);

        //rollLabel
        springLayout.putConstraint(SpringLayout.EAST, rollLabel, 0, SpringLayout.EAST, pwdLabel);
        springLayout.putConstraint(SpringLayout.NORTH, rollLabel, 20, SpringLayout.SOUTH, pwdLabel);

        //rollTe
        springLayout.putConstraint(SpringLayout.WEST, rollTet, 20, SpringLayout.EAST, rollLabel);
        springLayout.putConstraint(SpringLayout.NORTH, rollTet, 0, SpringLayout.NORTH, rollLabel);
        //loginBtn
        springLayout.putConstraint(SpringLayout.WEST, fanhuiBtn, 20, SpringLayout.EAST, rollLabel);
        springLayout.putConstraint(SpringLayout.NORTH, fanhuiBtn, 20, SpringLayout.SOUTH, rollLabel);

        //zhuceBtn
        springLayout.putConstraint(SpringLayout.WEST, zhuceBtn, 80, SpringLayout.EAST, fanhuiBtn);
        springLayout.putConstraint(SpringLayout.NORTH, zhuceBtn, 0, SpringLayout.NORTH, fanhuiBtn);
    }

    public JTextField getUserTxt() {
        return userTxt;
    }

    public JPasswordField getPwdField() {
        return pwdField;
    }

    public JTextField getroleTxt() {
        return rollTet;
    }
}
